/**
 * 成就系统配置
 * 定义所有可解锁的成就及其条件
 */
import type { Achievement } from '@/types/statistics';

/**
 * 成就触发条件类型
 */
export interface AchievementCondition {
  type: 'article_count' | 'word_count' | 'quality_score' | 'streak_days' | 'ai_usage' | 'template_usage' | 'inspiration_count' | 'community_action';
  threshold: number;
  description: string;
}

/**
 * 完整成就定义（包含触发条件）
 */
export interface AchievementDefinition extends Omit<Achievement, 'unlocked' | 'unlockedAt' | 'progress'> {
  conditions: AchievementCondition[];
}

/**
 * 成就目录
 */
export const ACHIEVEMENT_DEFINITIONS: AchievementDefinition[] = [
  // ========== 新手成就 (Beginner) ==========
  {
    id: 'ach_first_article',
    name: '初次尝试',
    description: '完成第一篇文章',
    icon: '🎉',
    category: 'beginner',
    rewards: {
      xp: 100,
      badge: '新手作者',
    },
    total: 1,
    conditions: [
      {
        type: 'article_count',
        threshold: 1,
        description: '完成1篇文章',
      },
    ],
  },
  {
    id: 'ach_first_1000_words',
    name: '千字文',
    description: '累计创作1000字',
    icon: '📝',
    category: 'beginner',
    rewards: {
      xp: 50,
    },
    total: 1000,
    conditions: [
      {
        type: 'word_count',
        threshold: 1000,
        description: '累计创作1000字',
      },
    ],
  },
  {
    id: 'ach_first_ai_usage',
    name: 'AI助手',
    description: '首次使用AI功能',
    icon: '🤖',
    category: 'beginner',
    rewards: {
      xp: 50,
    },
    total: 1,
    conditions: [
      {
        type: 'ai_usage',
        threshold: 1,
        description: '使用AI功能1次',
      },
    ],
  },
  {
    id: 'ach_first_template',
    name: '模板达人',
    description: '首次使用模板创作',
    icon: '📋',
    category: 'beginner',
    rewards: {
      xp: 50,
    },
    total: 1,
    conditions: [
      {
        type: 'template_usage',
        threshold: 1,
        description: '使用模板1次',
      },
    ],
  },

  // ========== 里程碑成就 (Milestone) ==========
  {
    id: 'ach_10_articles',
    name: '初露锋芒',
    description: '累计完成10篇文章',
    icon: '📚',
    category: 'milestone',
    rewards: {
      xp: 500,
      badge: '勤奋作者',
    },
    total: 10,
    conditions: [
      {
        type: 'article_count',
        threshold: 10,
        description: '完成10篇文章',
      },
    ],
  },
  {
    id: 'ach_50_articles',
    name: '创作达人',
    description: '累计完成50篇文章',
    icon: '🏆',
    category: 'milestone',
    rewards: {
      xp: 2000,
      badge: '创作达人',
    },
    total: 50,
    conditions: [
      {
        type: 'article_count',
        threshold: 50,
        description: '完成50篇文章',
      },
    ],
  },
  {
    id: 'ach_100_articles',
    name: '百篇著作',
    description: '累计完成100篇文章',
    icon: '👑',
    category: 'milestone',
    rewards: {
      xp: 5000,
      badge: '创作大师',
    },
    total: 100,
    conditions: [
      {
        type: 'article_count',
        threshold: 100,
        description: '完成100篇文章',
      },
    ],
  },
  {
    id: 'ach_10k_words',
    name: '万字作者',
    description: '累计创作10000字',
    icon: '✍️',
    category: 'milestone',
    rewards: {
      xp: 800,
    },
    total: 10000,
    conditions: [
      {
        type: 'word_count',
        threshold: 10000,
        description: '累计创作10000字',
      },
    ],
  },
  {
    id: 'ach_50k_words',
    name: '洋洋洒洒',
    description: '累计创作50000字',
    icon: '📖',
    category: 'milestone',
    rewards: {
      xp: 3000,
      badge: '多产作者',
    },
    total: 50000,
    conditions: [
      {
        type: 'word_count',
        threshold: 50000,
        description: '累计创作50000字',
      },
    ],
  },
  {
    id: 'ach_100k_words',
    name: '著作等身',
    description: '累计创作100000字',
    icon: '🎓',
    category: 'milestone',
    rewards: {
      xp: 8000,
      badge: '文豪',
    },
    total: 100000,
    conditions: [
      {
        type: 'word_count',
        threshold: 100000,
        description: '累计创作100000字',
      },
    ],
  },

  // ========== 质量成就 (Quality) ==========
  {
    id: 'ach_quality_80',
    name: '优秀作品',
    description: '获得一篇质量分80分以上的文章',
    icon: '⭐',
    category: 'quality',
    rewards: {
      xp: 200,
    },
    total: 1,
    conditions: [
      {
        type: 'quality_score',
        threshold: 80,
        description: '单篇质量分≥80',
      },
    ],
  },
  {
    id: 'ach_quality_90',
    name: '品质保证',
    description: '获得一篇质量分90分以上的文章',
    icon: '🌟',
    category: 'quality',
    rewards: {
      xp: 500,
      badge: '品质作者',
    },
    total: 1,
    conditions: [
      {
        type: 'quality_score',
        threshold: 90,
        description: '单篇质量分≥90',
      },
    ],
  },
  {
    id: 'ach_quality_95',
    name: '完美主义',
    description: '获得一篇质量分95分以上的文章',
    icon: '💎',
    category: 'quality',
    rewards: {
      xp: 1000,
      badge: '完美主义者',
    },
    total: 1,
    conditions: [
      {
        type: 'quality_score',
        threshold: 95,
        description: '单篇质量分≥95',
      },
    ],
  },

  // ========== 效率成就 (Efficiency) ==========
  {
    id: 'ach_3_days_streak',
    name: '三日之约',
    description: '连续创作3天',
    icon: '🔥',
    category: 'efficiency',
    rewards: {
      xp: 150,
    },
    total: 3,
    conditions: [
      {
        type: 'streak_days',
        threshold: 3,
        description: '连续创作3天',
      },
    ],
  },
  {
    id: 'ach_7_days_streak',
    name: '坚持不懈',
    description: '连续创作7天',
    icon: '🔥',
    category: 'efficiency',
    rewards: {
      xp: 400,
      badge: '坚持者',
    },
    total: 7,
    conditions: [
      {
        type: 'streak_days',
        threshold: 7,
        description: '连续创作7天',
      },
    ],
  },
  {
    id: 'ach_30_days_streak',
    name: '创作大师',
    description: '连续创作30天',
    icon: '💪',
    category: 'efficiency',
    rewards: {
      xp: 2000,
      badge: '自律大师',
    },
    total: 30,
    conditions: [
      {
        type: 'streak_days',
        threshold: 30,
        description: '连续创作30天',
      },
    ],
  },
  {
    id: 'ach_100_days_streak',
    name: '百日筑基',
    description: '连续创作100天',
    icon: '🏅',
    category: 'efficiency',
    rewards: {
      xp: 10000,
      badge: '传奇作者',
    },
    total: 100,
    conditions: [
      {
        type: 'streak_days',
        threshold: 100,
        description: '连续创作100天',
      },
    ],
  },

  // ========== 探索成就 (Exploration) ==========
  {
    id: 'ach_ai_10_times',
    name: 'AI新手',
    description: '使用AI功能10次',
    icon: '🤖',
    category: 'exploration',
    rewards: {
      xp: 200,
    },
    total: 10,
    conditions: [
      {
        type: 'ai_usage',
        threshold: 10,
        description: '使用AI功能10次',
      },
    ],
  },
  {
    id: 'ach_ai_50_times',
    name: 'AI达人',
    description: '使用AI功能50次',
    icon: '🚀',
    category: 'exploration',
    rewards: {
      xp: 800,
      badge: 'AI专家',
    },
    total: 50,
    conditions: [
      {
        type: 'ai_usage',
        threshold: 50,
        description: '使用AI功能50次',
      },
    ],
  },
  {
    id: 'ach_template_10_times',
    name: '模板收集者',
    description: '使用模板10次',
    icon: '📋',
    category: 'exploration',
    rewards: {
      xp: 300,
    },
    total: 10,
    conditions: [
      {
        type: 'template_usage',
        threshold: 10,
        description: '使用模板10次',
      },
    ],
  },
  {
    id: 'ach_inspiration_20',
    name: '灵感捕手',
    description: '收集20个灵感素材',
    icon: '💡',
    category: 'exploration',
    rewards: {
      xp: 400,
      badge: '灵感大师',
    },
    total: 20,
    conditions: [
      {
        type: 'inspiration_count',
        threshold: 20,
        description: '收集20个灵感素材',
      },
    ],
  },
  {
    id: 'ach_inspiration_100',
    name: '灵感宝库',
    description: '收集100个灵感素材',
    icon: '🎨',
    category: 'exploration',
    rewards: {
      xp: 1500,
      badge: '灵感收藏家',
    },
    total: 100,
    conditions: [
      {
        type: 'inspiration_count',
        threshold: 100,
        description: '收集100个灵感素材',
      },
    ],
  },

  // ========== 社区成就 (Community) ==========
  {
    id: 'ach_share_template',
    name: '乐于分享',
    description: '分享一个模板到社区',
    icon: '🎁',
    category: 'community',
    rewards: {
      xp: 300,
      badge: '分享达人',
    },
    total: 1,
    conditions: [
      {
        type: 'community_action',
        threshold: 1,
        description: '分享模板到社区',
      },
    ],
  },
  {
    id: 'ach_share_5_templates',
    name: '社区贡献者',
    description: '分享5个模板到社区',
    icon: '🌟',
    category: 'community',
    rewards: {
      xp: 1000,
      badge: '社区之星',
    },
    total: 5,
    conditions: [
      {
        type: 'community_action',
        threshold: 5,
        description: '分享5个模板',
      },
    ],
  },
];

/**
 * 根据分类获取成就
 */
export const getAchievementsByCategory = (category: Achievement['category']) => {
  return ACHIEVEMENT_DEFINITIONS.filter((ach) => ach.category === category);
};

/**
 * 根据ID获取成就定义
 */
export const getAchievementById = (id: string) => {
  return ACHIEVEMENT_DEFINITIONS.find((ach) => ach.id === id);
};

/**
 * 成就分类信息
 */
export const ACHIEVEMENT_CATEGORIES = {
  beginner: {
    label: '新手入门',
    icon: '🎓',
    color: '#52c41a',
    description: '开始你的创作之旅',
  },
  milestone: {
    label: '里程碑',
    icon: '🏆',
    color: '#1890ff',
    description: '重要的创作成就',
  },
  quality: {
    label: '品质追求',
    icon: '⭐',
    color: '#faad14',
    description: '创作高质量内容',
  },
  efficiency: {
    label: '效率大师',
    icon: '⚡',
    color: '#f5222d',
    description: '保持创作节奏',
  },
  exploration: {
    label: '探索发现',
    icon: '🔍',
    color: '#722ed1',
    description: '探索各种功能',
  },
  community: {
    label: '社区互动',
    icon: '🤝',
    color: '#13c2c2',
    description: '参与社区贡献',
  },
} as const;

/**
 * 等级系统配置
 */
export const LEVEL_CONFIG = {
  // 每级所需经验值（指数增长）
  getRequiredXP: (level: number): number => {
    if (level <= 1) return 0;
    // 基础经验 * (等级 ^ 1.5)
    return Math.floor(500 * Math.pow(level, 1.5));
  },

  // 等级对应的称号
  getLevelTitle: (level: number): string => {
    if (level < 5) return '新手作者';
    if (level < 10) return '初级作者';
    if (level < 20) return '熟练作者';
    if (level < 30) return '高级作者';
    if (level < 50) return '专业作者';
    if (level < 80) return '资深作者';
    if (level < 100) return '创作大师';
    return '传奇作者';
  },

  // 等级对应的特权
  getLevelPrivileges: (level: number): string[] => {
    const privileges: string[] = ['基础创作功能'];

    if (level >= 5) privileges.push('高级模板');
    if (level >= 10) privileges.push('多模型对比');
    if (level >= 15) privileges.push('AI额度提升');
    if (level >= 20) privileges.push('数据导出');
    if (level >= 30) privileges.push('优先支持');
    if (level >= 50) privileges.push('定制化服务');
    if (level >= 80) privileges.push('专属顾问');

    return privileges;
  },
};

/**
 * 经验值奖励配置
 */
export const XP_REWARDS = {
  ARTICLE_COMPLETED: 50, // 完成一篇文章
  WORD_MILESTONE_100: 5, // 每100字
  QUALITY_BONUS_80: 20, // 质量分80+
  QUALITY_BONUS_90: 50, // 质量分90+
  QUALITY_BONUS_95: 100, // 质量分95+
  DAILY_STREAK: 10, // 每连续一天
  AI_USAGE: 2, // 使用AI功能
  TEMPLATE_USAGE: 5, // 使用模板
  INSPIRATION_ADDED: 3, // 添加灵感
  TEMPLATE_SHARED: 50, // 分享模板
} as const;
